Sam's Guide to Programming
Guide to programming (Beginners) It has come to my attention that we are in need of more programmers if we want PoL to work, so I decided to dedicate some of my time to teaching you guys how to program a few useful languages Hopefully, by the time I am finished, you will have knowledge of the essentials and basics of programming and can go more into depth until you are a master at coding If you ever need help, feel free to message Sam Dreadrage Will soon make a detailed guide with multiple lessons on C++, Batch, Python, HTML, and more! Stay tuned! :) Should I continue with this or abandon it? Please comment and vote! Continue Abandon Languages Batch (Shell Linux) If you wish to learn programming C++ one day, it is good to begin with a simple language. One simple language is Batch, with CMD (Command Prompt) as the command line. A command line is the tool used to run the code. Batch is a Windows only code, much like Visual Basic To run batch, download either notepad++ (used for coding) Or simply user the normal 'notepad' on windows Once you have written your code, save it as "filename".bat .bat indicates that the file is a batch file, and will automatically be opened with Command Prompt. Just a useful tip, use Control + C at any time to terminate the batch file Lesson 1 Batch consists of many different elements, such as Labels (:A, :Label, :abc, etc), variables (set /a var1=, set /p var2=, etc), and as basically all languages, if and else statements (if %var1% 123 then... else...) If there is ever a command you need help with, type the command /? example: set /? or, you can just message me what you need help with :) Let me explain a few commands in batch echo (echo Hi) - prints text example echo What's up? would display What's up? in command prompt Rem (Rem this is a note) - displays a note not shown in the code example Rem Random note would display... nothing Another way to display notes is by using :: in front of the line This is useful if you wish to remove a line from being executed, but wish to keep it for reference or if needed in the future. Fun little code - color ## to change the text or background color of command prompt, you can use the command color ## with the desired colors. the first alphanumeric is the background color, the second is the text (don't bother memorizing these useless numbers and letters, just enter in color /? for the list List of colors: 0 - Black 1 - Blue 2- Green 3- Aqua 4- Red 5- Purple 6- Yellow 7- White 8 - Gray 9 - Light Blue A - Light Green B - Light Aqua C - Light Red D - Light Purple E - Light Yellow F - Bright White example White background, Light Yellow text would be color 7e and so forth Pause (Pause) - asks for user input to continue Using Pause asks for the user to hit any key to continue the code example echo What's up? Pause echo Not much would display What's up? Press any key to continue . . . Then once you pressed any key: Not much Pause is very useful to put at the end of a batch file, because if you do not, the code will exit right away for example echo What's up? Pause echo Not Much Right after you press a key to continue, it would exit immediatly because it takes a fraction of a second for echo Not Much to go through revised code echo What's up? Pause echo Not Much pause Cls (cls) - clears the screen You can use cls to clear the text of the screen example echo Hi there! pause cls echo Bye! pause would display Hi there! after user input: the screen would be cleared, and you would just see Bye! Lesson 2 In lesson 2 I will go further into depth with simple commands for text pause timers etc Echo off Echo off is essential to many batch files, mainly for format reasons. Whenever a line is executed, it shows C:\Users\Username> befoe the code, so if you typed echo Hi! it would really show C:\Users\Username> Hi! don't want this? Don't worry, it can be fixed with 2 simple words echo off add this to the beginning of the code to remove all that microsoft junk example: echo Microsoft junk :( pause cls echo I want it gone! pause would display C:\Users\Username> Microsoft junk :( Press any key to continue... [ Cleared page ] C:\Users\Username> I want it gone! Press any key to continue... One simple line can fix this: echo off echo Microsoft junk :( pause cls echo I want it gone! pause would display Microsoft junk :( Press any key to continue... [ Cleared page ] I want it gone! Press any key to continue... Title (title fun title :D) - changes the title of the batch file By default, the title of Command Prompt or a batch file is simply "Command Prompt" If you wish to change this, you can simply change the title of the page by entering in title TitleGoesHere example echo off title Testing title changing color 0f echo My first title change :) pause cls echo It worked! pause Echo. (Echo.) - displays a blank line Echo. basically skips a line example echo Hi echo. echo Notice I skipped a line? Pause displays Hi Notice I skipped a line? Start (start C:\Users\%Username%\Desktop\Test.bat) - starts a file To use the start command, simply type start followed by the file extension example to open something on desktop, just enter this code in, replace Test.bat with the exact name of the file with the file type (.doc, .txt, .bat, .exe, etc) start C:\Users\%Username%\Desktop\Test.bat Timeout (timeout 5) - wait for batch file to continue Timoeut is a very useful command that makes the file wait to execute the command The way timeout works is timeout # replace # with the number of seconds you wish for the timeout to last for timeout looks like this: timeout 3 would display Waiting for 3 seconds, press a key to continue... Waiting for 2 seconds, press a key to continue... Waiting for 1 second, press a key to continue... if you press a key during timeout, it skips the pause want to get around this so that the user cannot skip it? use /nobreak for example timeout 3 /nobreak would require the user to wait, pressing a key would not do anything timeout 3 /nobreak displays Waiting for 3 seconds... Waiting for 2 seconds... Waiting for 1 second... There are more complicated ways to add timeout, such as this code that does NOT display anything, it simply does not allow user input for the designated amount of time specified: timeout /t 1 /nobreak > NUL 1 can be replaced with any number you desire del (del file extension here) - delete files del C:\Users\%Username%\Desktop\Test.bat would delete the file Test.bat, located on your user's desktop Using del prompts the user for input of Y/N to confirm deletion This can be bypassed by using del /f /q example del /f /q C:\Users\%Username%\Desktop\Test.bat would automatically delete Test.bat wihout user input Lesson 3 For lesson 3, I will be going into labels, if and else statements, as well as set commands. This will most likely be the most complicated - and important lesson, so I hope you are ready to learn! --NOTE-- For some reason, labels are messed up on wiki, beginning a line with a colin indents, I cannot find a way to get around this, so please replace all indentions before Labels with colins (:). Thanks, and once again I apologize. Labels (:Label, :A, :B, :123abc, etc.) - Certain section of coding Labels are one of the most important codes in batch. They allow you to seperate parts of code, and go to certain sections. A label is just a colin : with a name after it. For example, :MyFirstLabel. Everything underneath :MyFirstLabel until the next label would be the section of code MyFirstLabel. Labels can be used as many times as you want, so long as each label has a different name. Example echo off color 0f title MyFirstLabels ::MyFirstLabel echo This is my first label! pause cls ::Abc123 echo this label is the Abc123 label! pause cls ::iLikePie101 echo this label is called iLikePie101. pause labels are executed from top to bottom, so MyFirstLabel would be executed first, followed by Abc123, then iLikePie101. If you want labels to be executed in different orders, you can use the goto command. Goto (goto MyFirstLabel, goto Testlabel123, goto 1337iscool) - jump over part of the code to execute it in the desired order Goto command allows you to jump over parts of the code to the designated label. Say you had 3 labels, Label1, Label2, and Label3. If you typed it out as Label1 then Label2 then Label3, but say you wanted it to be executed in the order of Label3, Label1, then Label2, you would use goto to do so. Goto is also extremely useful in if statements (will get into that later) to exit a batch file, simple add in goto error as long as error is not a valid label in the batch file, it will exit the code. example of no gotos echo off ::Label1 echo This is label 1 pause ::Label2 echo This is label 2 pause ::Label3 echo this is label 3 pause Now if you wanted it to go Label3, then 1, then 2, you would do this: echo off goto Label3 ::Label1 echo This is label 1 pause goto Label2 ::Label2 echo This is label 2 pause ::Label3 echo This is label 3 pause goto Label1 It is important to usually (unless you wish for it to go there without input) put goto after a pause, timeout, or set (will get into sets later) because if you do not, say you had echo Hi goto Label2, it would execute in miliseconds, and you would not see the echo Hi. Set (set /p, set /a, etc) - Set a variable The Set command is probably the most useful thing in batch. It allows you to set a variable to be used later in the code. To set a variable called say, "Var1", you would include Set /A Var1= To make it so that Var1 = 123ILikePie You would type Set /A Var1=123ILikePie To prompt the user to set the variable, you would type: Set /P Var1= Whatever the user types in before hitting enter would become Var1. To call a variable, you type %Var1%. Typing Echo %Var1% would print 123ILikePie (unless you use set /p, then it would print whatever the user entered.) You can have as many variables as you want, so long as they have different names. For example,: echo off cls set /a Banana= This is my first variable :) :A echo %banana% echo This is my first user input variable :) echo Enter in your favorite fruit set /p Fruit= set /a FavFruit=Mango cls echo Oh cool, you like %Fruit%, I love %fruit%s too, but they aren't my favorite fruit, my favorite fruit is and will always be %favfruit%! pause This would display This is my first variable :) Enter in your favorite fruit (Prompt for user input) (Clear everything) Oh cool, you like (if they entered in berries) berries, I love berries too, but they aren't my favorite fruit, my favorite fruit is and will always be Mango! You can add text after the "=" in a /p set, so that it displays text on that line. To do make it show I like pie (prompt for input) on same line, simply do something like: set /p pie=I like pie Before calling a variable, you must ALWAYS declare the variable with a set. If you don't, it will not recognize it as it has never seen the variable before, since code is executed top to bottom. If variables (if %var1% pie...) - If a variable equals a certain thing, execute a command) If commands for variables are extremely useful. It can be used to check if an input or such from a set command To do a command if an input is a certain thing, you would do something like this: echo off cls set /a food= Pie :A echo What is your favorite food? set /p fav= Food: :: the next line is what checks if it is equal to something if %fav% %food% goto B cls echo Cool, your favorite food is %fav%. Mine is %food% :B echo You like %fav%? cool, me too! pause Another example: echo off cls :asdf echo Enter in a number 1-5 set /p num=Number - if %num% 1 goto 1 if %num% 2 goto 2 if %num% 3 goto 3 if %num% 4 goto 4 if %num% 5 goto 5 echo That number isn't 1-5, please enter another number. timeout 5 goto asdf :1 echo Yeah, 1 is a cool number. pause :2 echo I like 2 too! pause :3 echo pfft... you like 3... pause :4 echo 4 is an okay number pause :5 echo I guess 5 is alright... pause If and Else (if ... else...) - Essential to complex codes, check if and elses If and Else is extremely essential. It can be used for many things, such as If a certain variable is something, do something, else do something different. It can be used to simplify codes like in the set command section. It could be modified like so: echo off cls set /a Banana= This is my first variable :A echo %banana% echo This is my first user input variable :) echo Enter in your favorite fruit set /p Fruit= set /a FavFruit=Mango cls if %Fruit% %FavFruit% ( echo I love Mangos too! ) ELSE ( echo Cool, I guess %Fruit% is okay, though I like %FavFruit% ) pause Now I will show you one last essential thing you need to know, and we can then create a complex batch file together, and you will be a master of batch! Random numbers (%random%) - Generates random numbers Random numbers is very useful in batch. Typing in %random% selects a random number from 1-32768. To echo a number 1-32768, simply type: %random% If you want to make it select from a different range of numbers, use the following code: %RANDOM% * 10 / 32768 This would select a number 0-9, but if you do not wish to include 9 and you wish to have a 10 instead, use: %RANDOM% * 10 / 32768 + 1 Now, in order to make it echo this, you need to use a set command, like so: echo off cls :One set /a rand=%RANDOM% * 10 / 32768 + 1 echo %rand% pause this would print the randomly selected number from 1-10. The reason this can be useful (I wouldn't be surprised if you quit reading this already due to the plain uselessness of this command so far) echo off cls :lolz set /a select=%RANDOM% * 3 / 32768 + 1 if %select% 1 goto 1 if %select% 2 goto 2 if %select% 3 goto 3 :1 echo The randomly selected number was 1. :2 echo The randomly selected number was 2. :3 echo The randomly selected number was 3. Put it all together! This is a batch file to make a guessing game that has points, and it randomly selects numbers. It includes basically everything you learned. Since labels glitch on wikia, I pasted it here, just copy it and paste it in notepad. http://justpaste.it/d7z1 The code (labels do not work on wikia unfortunately) :1 echo off cls set %Points%=5 set %Skips%=0 echo Loading timeout /t 1 /nobreak > NUL cls echo Loading Main Script... [ ] 0%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Level Selector.. 15%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Loading Points. 25%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Level Skips.. 65%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Shop Mainframe... 90%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Shop Items. 100%% Complete timeout /t 1 /nobreak > NUL cls echo Loading Complete pause goto 2 :2 cls color 0f echo Welcome to Guessing game! title Guessing Game echo Points: %Points% echo Please select a difficulty echo. echo 1 Easy echo 2 Hard echo 3 Exit set /p Choice= if %choice% 1 goto A1 if %choice% 2 goto B1 if %choice% 3 goto Exit :A1 cls color 0f SET /A N1=%RANDOM% * 10 / 32768 + 1 echo Guess a number from 1-10 echo. echo. set /P G1=Enter a number from 1-10: timeout /t /1 /nobreak > NUL IF %G1% home goto 2 IF %G1% %N1% ( goto A2 ) ELSE ( goto A3 ) :A2 cls echo You guessed %G1%... echo Loading... timeout /t 3 /nobreak > NUL cls echo The answer was %N1%. You win! color 0a set /a Points=(%Points% + 1) timeout /t 3 /nobreak > NUL goto A1 :A3 cls echo You guessed %G1%... echo Loading... timeout /t 3 /nobreak > NUL cls echo The answer was %N1%. You lose! color 0c timeout /t 3 /nobreak > NUL goto A1 :B1 cls color 0f SET /A N2=%RANDOM% * 50 / 32768 + 1 echo Guess a number from 1-50 echo. echo. set /P G2=Enter a number from 1-50: timeout /t /1 /nobreak > NUL IF %G2% home goto 2 IF %G2% %N2% ( goto B2 ) ELSE ( goto B3 ) :B2 cls echo You guessed %G2%... echo Loading... timeout /t 3 /nobreak > NUL cls echo The answer was %N2%. You win! color 0a set /a Points=(%Points% + 1) timeout /t 3 /nobreak > NUL goto B1 :B3 cls echo You guessed %G2%... echo Loading... timeout /t 3 /nobreak > NUL cls echo The answer was %N2%. You lose! color 0c timeout /t 3 /nobreak > NUL goto B1 Congratulations, you are now a master of batch programming! Back to top Category:Guides